<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/12/21
 * Time: 下午7:48
 */
use Overtrue\Wechat\Staff;
class WeChatStaffController extends BaseController
{
    public function send_msg(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'openid' => 'required',
                'msg' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $openid = Input::get('openid');
            $msg = Input::get('msg');

            //验证token
            //需要修改为微博调用用户的token验证
//            $engineer_data = MaintenanceEngineers::getEngineerByToken($token);
//            if(is_numeric($engineer_data) && $engineer_data < 0){
//                $response = Lang::get("sce_error.user_token_not_exist_1004");
//                break;
//            }
            $staff = new Staff(Config::get('wechat.APP_ID'), Config::get('wechat.APP_SECRET'));

            try {
                $staff->send($msg)->to($openid);
                $response = array('code' => 0, 'msg' => 'ok', 'result' => '发送成功！');
            } catch (Exception $e) {
                $response = array('code' => 0, 'msg' => 'ok', 'result' => '发送失败：' . $e->getMessage());
            }
        } while (false);
        return json_encode($response);
    }
}